package cn.e4j.modular.crm.controller;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.core.util.StringUtils;
import cn.e4j.modular.crm.entity.GwedT;
import cn.e4j.modular.crm.service.impl.Gwei006ServiceImpl;
import cn.e4j.core.util.NumToRMB;
import cn.e4j.modular.erp.controller.ErpLogic;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Scanner;

/**
 * 开票通知
 * @author liuw
 * @Date 2019/09/04
 */
@Controller
@RequestMapping ("/crm/gwei006")
public class Gwei006Controller {

    @Autowired
    private Gwei006ServiceImpl gwei006Service;

    @Autowired
    private ErpLogic erpLogic;

    /**
     * 跳转开票通知页面
     */
    @RequiresPermissions("crm:gwei006:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwei006.html";
    }


    /**
     * 查询开票通知列表
     */
    @RequestMapping("/getList")
    @RequiresPermissions("crm:gwei006:init")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();

        ShiroUser user = ShiroKit.getUser();
        String ID = user.getID();
        String LANG = LangUtils.getLang(user.getLANG());
        String ENT = user.getDBC();
        inDto.put("USER_ID",ID);
        inDto.put("LANG",LANG);
        inDto.put("ENT",ENT);

        List<Dto> result = gwei006Service.getList(page, inDto);
        return new PageReData(page, result);
    }


    /**
     * 获取出货单号列表
     */
    @RequestMapping("/getGwecData")
    @ResponseBody
    public ResponseData getGwecData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();

        String LANG = LangUtils.getLang(shiroUser.getLANG());
        String DBC = shiroUser.getDBC();
        String USER_ID = shiroUser.getID();

        inDto.put("ENT", DBC);
        inDto.put("LANG", LANG);
        inDto.put("USER_ID", USER_ID);

        if(!StringUtils.isEmpty(inDto.getString("gweclist"))){
            String[] str = inDto.getString("gweclist").split(",");
            List<String> plist = Lists.newArrayList();
            for (String gwec : str) {
                plist.add(gwec);
            }
            inDto.put("GWECLIST",plist);
        }
        List<Dto> resultDto = gwei006Service.getGwecPage(page,inDto);

        // 对结果进行处理
        for (int i = 0; i < resultDto.size(); i++) {
            Dto result = resultDto.get(i);
            Dto tempDto = Dtos.newDto();
            tempDto.put("GWCSDOCNO",result.getString("GWEC003"));
            tempDto.put("GWCSSEQ",result.getString("GWEC101"));
            tempDto.put("GWCSENT",shiroUser.getDBC());
            Dto gwcs = gwei006Service.getGwcsData(tempDto);
            if(gwcs != null) {
                result.put("GWAP100",gwcs.getString("GWCS004"));
            }
        }

        return new PageReData(page,resultDto);
    }


    /**
     * 开票通知单身表格数据
     */
    @RequestMapping("/getDetailList")
    @ResponseBody
    public ResponseData getDetailList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        ShiroUser user = ShiroKit.getUser();

        inDto.put("ENT", user.getDBC());
        inDto.put("LANG", LangUtils.getLang(user.getLANG()));

        List<Dto> resultDto = gwei006Service.getGwedData(inDto);

        // 获取营运中心名称
        inDto.put("BIZ_CODE",resultDto.get(0).getString("GWEDPLANT"));

        Dto azpDto = null;
        try {
            azpDto = erpLogic.getERPService("getAzpFile",inDto);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }

        if(azpDto != null) {
            resultDto.get(0).put("GWEDPLANT_NAME", azpDto.getString("AZP02"));
        }else {
            resultDto.get(0).put("GWEDPLANT_NAME", "");
        }

        return new SuccessResponseData(resultDto);
    }

    /**
     * 新增
     */
    @RequestMapping("/addData")
    @RequiresPermissions("crm:gwei006:add")
    @ResponseBody
    public ResponseData addData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwedT> gwedTList = inDto.getReqList(GwedT.class);

        gwei006Service.addData(gwedTList);
        return new SuccessResponseData();
    }

    /**
     * 修改
     */
    @RequestMapping("/editData")
    @RequiresPermissions("crm:gwei006:edit")
    @ResponseBody
    public ResponseData editData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwedT> gwedTList = inDto.getReqList(GwedT.class);
        gwei006Service.editData(gwedTList);
        return new SuccessResponseData();
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwei006:del")
    @ResponseBody
    public ResponseData del(GwedT gwed) {
        gwei006Service.delete(gwed);
        return new SuccessResponseData();
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwei006:del")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwedT> gwedTList = inDto.getReqList(GwedT.class);
        gwei006Service.deleteBatch(gwedTList);
        return new SuccessResponseData();
    }


    /**
     * 审核
     */
    @RequestMapping("/checkDataBatch")
    @RequiresPermissions("crm:gwei006:del")
    @ResponseBody
    public ResponseData review(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwedT> gwedTList = inDto.getReqList(GwedT.class);
        gwei006Service.review(gwedTList);
        return new SuccessResponseData();
    }

    /**
     * 取消审核
     */
    @RequestMapping("/cancelCheckDataBatch")
    @RequiresPermissions("crm:gwei006:del")
    @ResponseBody
    public ResponseData cancelReview(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwedT> gwedTList = inDto.getReqList(GwedT.class);
        Dto outDto = gwei006Service.cancelReview(gwedTList);
        if(outDto.getAppCode() == 500) {
            return new ErrorResponseData(outDto.getAppMsg());
        }
        return new SuccessResponseData();
    }

    /**
     * 编辑页面 - 新增时获取默认数据 (生成单号，申请人员，归属组织)
     */
    @RequestMapping("/getAddData")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto addDataDto = Dtos.newDto();

        addDataDto.put("GWED001",shiroUser.getID());  // 申请人员
        addDataDto.put("GWED001_NAME",shiroUser.getNAME());  // 申请人员
        addDataDto.put("GWED101",shiroUser.getORG_ID()); // 组织
        addDataDto.put("GWED101_NAME",shiroUser.getSysOrgT().getNAME());  // 归属组织
        addDataDto.put("GWEDDOCNO","KPSQ" + DateUtils.getDate("yyyyMMddSSSSS")); // 单号
        return new SuccessResponseData(addDataDto);
    }


    /**
     * 根据申请人员获取归属组织
     */
    @RequestMapping("/getGwed101")
    @ResponseBody
    public ResponseData getGwed101(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto resultDto = gwei006Service.getGwed101(inDto);
        return new SuccessResponseData(resultDto);
    }

    @RequiresPermissions("crm:gwei006:print")
    @RequestMapping("print")
    @ResponseBody
    public ResponseData print(HttpServletRequest request, HttpServletResponse response,
                        ModelMap map,@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();

        List<Dto> gwed_list = this.gwei006Service.getGwedByDoc(inDto);

        // 金额总计
        Dto total = this.gwei006Service.getGwed011Sum(inDto);

        Dto gwed = gwed_list.get(0);
        gwed.put("ENT",shiroUser.getDBC());
        // 从客户资料中获取开票资料
        gwed.putAll(this.gwei006Service.getGwak(gwed));
        gwed.put("TOTAL",total.getString("GWED011_SUM"));
        Scanner in = new Scanner(total.getString("GWED011_SUM"));
        Double inmun = in.nextDouble();
        // 金额格式转换（数字转汉字）
        NumToRMB nr = new NumToRMB();
        String totol_rmb = nr.toHanStr(nr.toNum(inmun))+"元"+nr.toHanStr1(nr.divide(inmun)[1]);
        gwed.put("TOTAL_RMB",totol_rmb);

        Dto outDto = Dtos.newDto();
        outDto.put("gwed",gwed);
        outDto.put("gwed_list",gwed_list);
        return new SuccessResponseData(outDto);
    }
}

